﻿package net.fishbowlmedia.scroller3d.control 
{
	import net.fishbowlmedia.scroller3d.model.MoviePlaneModel;
	
	import com.caurina.transitions.Tweener;
	
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.primitives.Plane;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	/**
	* ...
	* @author Tom Lauck
	*/
	public class CarouselCoverControl extends MoviePlaneControl
	{
		private var _tweenTransition:String;
		private var _startingCoverId:int;
		private var _selectedCoverOffest:int;
		private var _selectedCoverScale:Number;
		private var _coverAngle:Number;	
		private var _tweenTime:Number;
		private var _visibleCoverLimit:int;
		
		public function CarouselCoverControl() 
		{
			selectedPlaneId = 1;
			addEventListener("PlaneClick", onPlaneClick);
		}
		public function addPlaneToScene(id:int, targetMovie:MovieClip):void
		{
			moviePlanes[id] = createMoviePlane(id, targetMovie);	
			scene.addChild(moviePlanes[id].plane);
			tweenPlaneById(id);
		}
		public function tweenPlaneById(id:int):void
		{	
			var plane:Plane = moviePlanes[id].plane as Plane;	
			var movieMaterial:MovieMaterial = plane.material as MovieMaterial;
			var movie:MovieClip = movieMaterial.movie as MovieClip;			
			
			var planeX:Number = Math.cos(id * coverAngle) * sceneWidth;
			var planeZ:Number = Math.sin(id * coverAngle) * sceneWidth;			
			var planeScale:Number = 1;
			var planeTweenTime = tweenTime;
			
			if (id == selectedPlaneId)
			{
				selectedMoviePlane = moviePlanes[id];				
				
				planeX = planeX + selectedCoverOffest;
				//planeScale = planeScale + selectedCoverScale;
			}
			
			movieMaterial.smooth = true;
			movieMaterial.interactive = true;
			movieMaterial.animated = true;
			movieMaterial.doubleSided = true;
						
			Tweener.addTween
			(
				plane, 
				{
					x: planeX,
					z: planeZ,
					scale:planeScale,
					time:planeTweenTime,
					transition:tweenTransition,
					onComplete:function()
					{
						if (id == totalPlanes - 1)
							keyboardCache = 0;
					}
				}
			);
			
		}
		private function loopPlaneTween():void
		{
			for(var i:int = 0; i < totalPlanes; i++)
			{
				tweenPlaneById(i);
			}
		}
		public function scrollRight():void
		{		
			loopPlaneTween();
		}
		
		public function scrollLeft():void
		{			
			loopPlaneTween();
		}
		public function scrollPaneById(id:int):void
		{
			
		}
		private function onPlaneClick(e:Event):void
		{
			scrollPaneById(planeClickId);
		}
		
		private var keyboardCache:int = 0;
		public function onKeyDown(e:KeyboardEvent) {			
			if (keyboardCache <= startingCoverId)
			{
				switch (e.keyCode) {
					case Keyboard.LEFT:
						scrollRight();
						break;
					case Keyboard.RIGHT:
						scrollLeft();
						break;
					case Keyboard.UP:
					case Keyboard.PAGE_UP:
						scrollLeft();
						break;					
					case Keyboard.DOWN:					
					case Keyboard.PAGE_DOWN:
						scrollRight();
						break;		
				}
				keyboardCache ++;
			}
		}		
		public function onMouseWheel(e:MouseEvent):void
		{
			if (e.delta < 0) 
				scrollRight();
			else 
				scrollLeft();
		}		
		//getters/setters
		public function get tweenTransition():String { return _tweenTransition; }
		
		public function set tweenTransition(value:String):void 
		{
			_tweenTransition = value;
		}
		
		public function get startingCoverId():int { return _startingCoverId; }
		
		public function set startingCoverId(value:int):void 
		{
			_startingCoverId = value;
		}
				
		public function get selectedCoverOffest():int { return _selectedCoverOffest; }
		
		public function set selectedCoverOffest(value:int):void 
		{
			_selectedCoverOffest = value;
		}
		
		public function get selectedCoverScale():Number { return _selectedCoverScale; }
		
		public function set selectedCoverScale(value:Number):void 
		{
			_selectedCoverScale = value;
		}
		
		public function get coverAngle():Number { return _coverAngle; }
		
		public function set coverAngle(value:Number):void 
		{
			_coverAngle = value;
		}
		
		public function get tweenTime():Number { return _tweenTime; }
		
		public function set tweenTime(value:Number):void 
		{
			_tweenTime = value;
		}
	}
	
}